package com.example.lanqiaodemo1.LanQiao;

public class _有理数类0505 {
    static class R{
        private long ra;
        private long rb;

        //最大公约数
        private long gcd(long a, long b) {
            if (b == 0)
                return a;
            return gcd(b, a % b);
        }

        //约分
        public R(long a,long b){
            ra=a;
            rb=b;
            long k=gcd(ra,rb);
            if (k>1){
                ra/=k;
                rb/=k;
            }
        }

        //加法
        public R add(R x){
            return new R(this.ra*x.rb+this.rb*x.ra,this.rb*x.rb);
        }

        //乘法
        public R mul(R x){
            return new R(ra*x.ra,rb*x.rb);
        }


        //打印R类型对象时会直接显示以下格式
        @Override
        public String toString() {
             if (rb==1)
                 return ""+ra;
             return ""+ra+"/"+rb;
        }


    }

    public static void main(String[] args) {
        R r = new R(1,3);
        System.out.println(r);
    }

}
